package org.example.memora.configur;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;

import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Primary;


/**
 * @author 丁利军
 * @date 2025年05月27日 22:28
 * @description TODO: 描述
 */
@Slf4j
@Configuration
public class RabbitConfig {

    public static final String QUEUE_NAME1 = "chat.message.queue";
    public static final String EXCHANGE_NAME = "chat.message.exchange";
    public static final String ROUTING_KEY = "chat.message";
    public static final String QUEUE_NAME = "chat.message.queue.v2";



    @Bean
    public DirectExchange deadLetterExchange() {
        return new DirectExchange("chat.message.dlx");
    }

    @Bean
    public Queue deadLetterQueue() {
        return new Queue("chat.message.dlx.queue", true);
    }

    @Bean
    public Binding deadLetterBinding() {
        return BindingBuilder.bind(deadLetterQueue()).to(deadLetterExchange()).with("chat.message.dlx");
    }


//    @Bean
//    public Queue chatQueue() {
//        return new Queue(QUEUE_NAME, true);
//    }
    @Bean
    public Queue chatQueue() {
        return QueueBuilder.durable(RabbitConfig.QUEUE_NAME)
                .withArgument("x-dead-letter-exchange", "chat.message.dlx")
                .withArgument("x-dead-letter-routing-key", "chat.message.dlx")
                .build();
    }

    @Bean
    public DirectExchange chatExchange() {
        return new DirectExchange(EXCHANGE_NAME);
    }

    @Bean
    public Binding binding() {
        return BindingBuilder.bind(chatQueue())
                .to(chatExchange())
                .with(ROUTING_KEY);
    }
    @Bean
    public MessageConverter jsonMessageConverter() {
        return new Jackson2JsonMessageConverter();
    }

    // 2. 用 JSON 转换器初始化 RabbitTemplate
    @Bean
    @Primary
    public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory,
                                         MessageConverter messageConverter) {
        RabbitTemplate template = new RabbitTemplate(connectionFactory);
        template.setMessageConverter(messageConverter);
        return template;
    }
    @Bean
    public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory(
            ConnectionFactory connectionFactory,
            MessageConverter messageConverter) {
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory);
        factory.setMessageConverter(messageConverter);
        factory.setErrorHandler(t -> log.error("【Rabbit消费异常】", t));
        factory.setConcurrentConsumers(3);
        factory.setMaxConcurrentConsumers(10);
        return factory;
    }

}

